/* wx的调用接口公用方法 */
import { success, SUCCESS } from './success';
import { start, START } from './start';
import { fail, FAIL } from './fail';
export default function wxFetch(action, url, params, extra = {}, callback) {
    return (dispatch) => {
        if (!action) return;
        dispatch(start(action, { params, fetchResult: {}, ...extra }));
        callback && callback(START);
        wx.request({
            url,
            data: JSON.stringify(params),
            method: 'POST',
            success(response = {}) {
                const fetchResult = response.data;
                if (fetchResult && fetchResult.code > 0) {
                    dispatch(success(action, { params, fetchResult, ...extra }));
                    callback && callback(SUCCESS, fetchResult);
                } else {
                    dispatch(fail(action, { params, fetchResult: {}, ...extra }));
                    callback && callback(FAIL, fetchResult);
                }
            },
            fail(response = {}) {
                const fetchResult = response.data;
                callback && callback(FAIL, fetchResult);
            },
        });
    };
}
